package com.css.fxfzypg.modules.saresults.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.constants.FxfzConstants;
import com.css.fxfzypg.modules.saresults.entity.DzqhApAsicMapEntity;
import com.css.fxfzypg.modules.saresults.entity.DzqhQpBasicMapEntity;
import com.css.fxfzypg.modules.saresults.entity.DzqhXqhBasicMapEntity;
import com.css.fxfzypg.modules.saresults.param.DrillHoleQueryParam;
import com.css.fxfzypg.modules.saresults.repository.*;
import com.css.fxfzypg.modules.saresults.service.KzsfResultsService;
import com.css.fxfzypg.modules.saresults.service.QpResultsService;
import com.css.fxfzypg.modules.saresults.service.SaResultsService;
import com.css.fxfzypg.modules.saresults.service.XqhResultsService;
import com.css.fxfzypg.modules.saresults.vo.DrillHoleQueryVo;
import com.css.fxfzypg.modules.saresults.vo.SaResultsHlVo;
import com.css.fxfzypg.modules.saresults.vo.SaResultsVo;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import com.css.fxfzypg.zcpt.sys.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class KzsfResultsServiceImpl implements KzsfResultsService {

    @Autowired
    private KzsfResultsRepository kzsfResultsRepository;

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SUserService sUserService;

    @Autowired
    private QpResultsServiceImpl qpResultsService;

    @Autowired
    private SaResultsServiceImpl saResultsService;

    @Autowired
    private XqhResultsServiceImpl xqhResultsService;

    @Autowired
    private QpResultsRepository qpResultsRepository;

    @Autowired
    private SaResultsRepository saResultsRepository;

    @Autowired
    private XqhResultsRepository xqhResultsRepository;

    @Autowired
    private DrillHoleQueryRepository drillHoleQueryRepository;


    @Override
    public RestResponse getKzsfResultsProject(DzqhQpBasicMapEntity qpBasicMapEntity, HttpServletRequest request,String geom) {
        try {
            int page = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int size = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            String userId = PlatformSessionContext.getUserID();
            String role = roleService.getRoleByUserId(userId);
            String user = sUserService.getSUser(PlatformSessionContext.getUserID());
            SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
            //单位id
            String orgId = sUser.getOrgId();
            //省级id
            String provinceId = sUser.getProvinceId();
            List<Map> roleMaps = JSONObject.parseArray(role, Map.class);
            ArrayList<String> roleList = new ArrayList<>();
            for (Map roleMap : roleMaps) {
                String roleCode = (String) roleMap.get("roleCode");
                roleList.add(roleCode);
            }
            if (roleList.contains(FxfzConstants.DZQH_GL_ROLE)) {
                //系统管理员
                qpBasicMapEntity.setRole("0");
            } else if (roleList.contains(FxfzConstants.DZQH_PRI_GL_ROLE)) {
                //省级管理员
                qpBasicMapEntity.setRole("1");
            } else if (roleList.contains(FxfzConstants.DZQH_GAME_GL_ROLE)) {
                //局级管理员
                qpBasicMapEntity.setRole("2");
            } else {
                //普通用户
                qpBasicMapEntity.setRole("3");
            }
            HashMap<String, Object> map = kzsfResultsRepository.getKzsfResultsProject(qpBasicMapEntity, page, size, orgId, provinceId, userId,geom);
            return RestResponse.succeed(map);
        } catch (Exception e) {
            String errorMessage = "抗震设防项目信息查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getKzsfResultsHl(DrillHoleQueryParam param, HttpServletRequest request, String geom) {
        try {
            int page = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int size = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            String userId = PlatformSessionContext.getUserID();
            String role = roleService.getRoleByUserId(userId);
            String user = sUserService.getSUser(PlatformSessionContext.getUserID());
            SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
            //单位id
            String orgId = sUser.getOrgId();
            //省级id
            String provinceId = sUser.getProvinceId();
            List<Map> roleMaps = JSONObject.parseArray(role, Map.class);
            ArrayList<String> roleList = new ArrayList<>();
            for (Map roleMap : roleMaps) {
                String roleCode = (String) roleMap.get("roleCode");
                roleList.add(roleCode);
            }
            if (roleList.contains(FxfzConstants.DZQH_GL_ROLE)) {
                //系统管理员
                param.setRole("0");
            } else if (roleList.contains(FxfzConstants.DZQH_PRI_GL_ROLE)) {
                //省级管理员
                param.setRole("1");
            } else if (roleList.contains(FxfzConstants.DZQH_GAME_GL_ROLE)) {
                //局级管理员
                param.setRole("2");
            } else {
                //普通用户
                param.setRole("3");
            }
            return RestResponse.succeed(kzsfResultsRepository.getKzsfResultsHl(param, page, size, orgId, provinceId, userId,geom));
        } catch (Exception e) {
            String errorMessage = "抗震设防孔位信息查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getKzsfResultsById(String projectId, String modelName) {
        SaResultsVo saResultsVo = new SaResultsVo();
        HashMap<String, Object> map = new HashMap<>();
        try {
            if(modelName.equals(FxfzConstants.DZQH_AP_MODEL)){
                DzqhApAsicMapEntity apAsicMapEntity = saResultsRepository.queryDzqhApAsicMap(projectId);
                saResultsService.queryFile(map, apAsicMapEntity.getProjectId());
                saResultsVo.setDzqhApAsicMapEntity(apAsicMapEntity);
            }
            if(modelName.equals(FxfzConstants.DZQH_QP_MODEL)){
                DzqhQpBasicMapEntity qpBasicMapEntity = qpResultsRepository.queryDzqhQpBasicMap(projectId);
                qpResultsService.queryFile(map, qpBasicMapEntity.getProjectId());
                saResultsVo.setDzqhQpBasicMapEntity(qpBasicMapEntity);
            }
            if(modelName.equals(FxfzConstants.DZQH_XQH_MODEL)){
                DzqhXqhBasicMapEntity xqhBasicMapEntity = xqhResultsRepository.queryDzqhXqpBasicMap(projectId);
                xqhResultsService.queryFile(map, xqhBasicMapEntity.getProjectId());
                saResultsVo.setDzqhXqhBasicMapEntity(xqhBasicMapEntity);
            }
            saResultsVo.setDzqhApCruxDhEntityList(saResultsRepository.queryDzqhApCruxDh(projectId));
            saResultsVo.setDzqhApCruxElEntityList(saResultsRepository.queryDzqhApCruxEl(projectId));
            saResultsVo.setDzqhApCruxHecEntityList(saResultsRepository.queryDzqhApCruxHec(projectId));
            saResultsVo.setDzqhApCruxLayerPfEntityList(saResultsRepository.queryDzqhApCruxLayerPf(projectId));
            saResultsVo.setDzqhApCruxScEntityList(saResultsRepository.queryDzqhApCruxSc(projectId));
            saResultsVo.setDzqhApCruxSdEntityList(saResultsRepository.queryDzqhApCruxSd(projectId));
            saResultsVo.setDzqhApCruxSgmEntityList(saResultsRepository.queryDzqhApCruxSgm(projectId));
            saResultsVo.setFileMap(map);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
        return RestResponse.succeed(saResultsVo);
    }

    @Override
    public void exportExcelKzsfResults(HttpServletResponse response, String projectId, String modelName) {
        try {
            if(modelName.equals(FxfzConstants.DZQH_AP_MODEL)){
                saResultsService.exportExcelSaResults(response,projectId);
            }
            if(modelName.equals(FxfzConstants.DZQH_QP_MODEL)){
                qpResultsService.exportExcelQpResults(response,projectId);
            }
            if(modelName.equals(FxfzConstants.DZQH_XQH_MODEL)){
                xqhResultsService.exportExcelXqhResults(response,projectId);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    @Override
    public void exportExcel(HttpServletResponse response, DrillHoleQueryParam param) {
        try {
            String userId = PlatformSessionContext.getUserID();
            String role = roleService.getRoleByUserId(userId);
            String user = sUserService.getSUser(PlatformSessionContext.getUserID());
            SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
            //单位id
            String orgId = sUser.getOrgId();
            //省级id
            String provinceId = sUser.getProvinceId();
            List<Map> roleMaps = JSONObject.parseArray(role, Map.class);
            for (Map roleMap : roleMaps) {
                String roleCode = (String) roleMap.get("roleCode");
                if (roleCode.equals(FxfzConstants.DZQH_GL_ROLE)) {
                    //系统管理员
                    param.setRole("0");
                } else if (roleCode.equals(FxfzConstants.DZQH_PRI_GL_ROLE)) {
                    //省级管理员
                    param.setRole("1");
                } else if (roleCode.equals(FxfzConstants.DZQH_GAME_GL_ROLE)) {
                    //局级管理员
                    param.setRole("2");
                } else {
                    //普通用户
                    param.setRole("3");
                }
            }
            List<DrillHoleQueryVo> data = kzsfResultsRepository.getKzsfResultsHl(param, orgId, provinceId, userId);
//            List<String> ids = kzsfResultsRepository.getDataId(userId, orgId, provinceId, param);
//            DrillHoleQueryParam drillHoleQueryParam = new DrillHoleQueryParam();
//            drillHoleQueryParam.setIds(ids);
//            List<DrillHoleQueryVo> data = drillHoleQueryRepository.getData(drillHoleQueryParam);
//            data.forEach(it->{
//                if(StringUtils.isBlank(it.getFieldType())){
//                    it.setFieldType("无");
//                }
//            });
            FileUtil.exportExcel(data, "钻孔信息", "钻孔信息", DrillHoleQueryVo.class, "钻孔信息.xlsx", response);
        } catch (Exception e) {
            String errorMessage = "抗震设防钻孔导出失败!";
            log.error(errorMessage, e);
        }
    }
}
